package com.geekchique.runningbuddy.activity;

import java.util.Calendar;
import java.util.GregorianCalendar;

import com.geekchique.runningbuddy.R;
import android.content.Context;
import android.content.Intent;
import android.content.IntentReceiver;
import android.location.Location;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.Spinner;

import com.geekchique.runningbuddy.controller.IMapOfJogsController;
import com.geekchique.runningbuddy.controller.MapOfJogsControllerImpl;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

public class MapOfJogs extends MapActivity {

	private IMapOfJogsController mapOfJogController;
	public LocationManager locationManager = null;
    protected LocationProvider provider;
	protected MyIntentReceiver myIntentReceiver = null;
	private static final String PROVIDERNAME = "testjog";
	private static final String TAG = "MAPOFJOG";
	final Handler mHandler = new Handler();
	final int MAPOFJOG = 3333;
	Bundle extras;

  @Override
  public void onCreate(Bundle icicle) {
	  super.onCreate(icicle);

	  int day = 0;
	  int month = 0;
	  int year = 0;

      this.setContentView(R.layout.map_of_jogs_view);

      locationManager =  (LocationManager)getSystemService(Context.LOCATION_SERVICE);

      mapOfJogController = new MapOfJogsControllerImpl(this);
      extras = getIntent().getExtras();

      if(extras == null) {
    	  Calendar cal = new GregorianCalendar();
    	  day = cal.get(Calendar.DAY_OF_WEEK);
    	  month = cal.get(Calendar.MONTH);
    	  year = cal.get(Calendar.YEAR);
      }else{
    	  day = extras.getInt("day");
    	  month = extras.getInt("month");
    	  year = extras.getInt("year");
      }


      mapOfJogController.createMapOfJogView(
    		  (MapView)findViewById(R.id.map),
    		  (Spinner)findViewById(R.id.spinner1),
    		  android.R.layout.simple_spinner_dropdown_item,
    		  (Button)findViewById(R.id.go), day, month, year);

  	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch(keyCode){
		case KeyEvent.KEYCODE_BACK:
			finish();
			return true;
		}
		return mapOfJogController.onKeyDown(keyCode, event);
	}



	class MyIntentReceiver extends IntentReceiver {

		@Override
		public void onReceiveIntent(Context context, Intent intent) {
			Log.v(TAG,"Updating a jogging a current jogging route.");
			Location location = locationManager.getCurrentLocation(PROVIDERNAME);
			Log.i(TAG, "Updating longitude: "+location.getLongitude() + ", latitude: "+location.getLatitude());
			mapOfJogController.setCurrentGPSLocationOfPhone(location);
			mapOfJogController.updateRunningPath();
		}
	}

}